package com.fjl.filewriter;

import org.json.JSONObject;

import com.android.apksigner.ApkSignerTool;
import com.bihe0832.packageinfo.bean.ApkInfo;
import com.bihe0832.packageinfo.getSignature.GetSignature;
import com.bihe0832.packageinfo.utils.ApkUtil;


public class ApkSignerTest
{

	public static void main(String[] args)
	{
		ApkSignerTool apkTool = new ApkSignerTool();
		
//		String apkInfo = apkTool.verify("C:/packer-release.apk");
//		String apkInfo = apkTool.verify("C:/第三次土地利用外业调查系统20180816未上传.apk");
//		String apkInfo = apkTool.verify("C:/MapGISExplorer.apk");
		String apkInfo = apkTool.verify("F:/MapGISExplorer.apk");
//		String apkInfo = apkTool.verify("F:/第三次土地利用外业调查系统20180816未上传.apk");
//		String apkInfo = apkTool.verify("F:/sharesample-release.apk");
//		String apkInfo = apkTool.verify("F:/MapGIS国土资源移动执法监察系统.apk");
//		String apkInfo = apkTool.verify("F:/移动一张图.apk");
		System.out.println(apkInfo);
		
//		getApkInfo("C:/第三次土地利用外业调查系统20180816未上传.apk");
//		getApkInfo("C:/packer-release.apk");
//		getApkInfo("C:/MapGISExplorer.apk");
		getApkInfo("F:/packV2.apk");
		
	}
	
	private static ApkInfo getApkInfo(String filePath){
		ApkInfo info = new ApkInfo();
		try {
			ApkUtil.getApkInfo(filePath, info);
		} catch(Exception e){
			showFailedCheckResult("RET_GET_INFO_BAD","get channel and apkinfo failed, throw an Exception");
			return null;
		}
		String v2Signature = ApkSignerTool.verify(filePath);
		
		try{
			JSONObject jsonobject = new JSONObject(v2Signature);  
			info.isV1SignatureOK = jsonobject.getBoolean(ApkSignerTool.KEY_RESULT_IS_V1_OK);
			info.isV2Signature = jsonobject.getBoolean(ApkSignerTool.KEY_RESULT_IS_V2);
			info.isV2SignatureOK = jsonobject.getBoolean(ApkSignerTool.KEY_RESULT_IS_V2_OK);
			info.v2CheckErrorInfo = jsonobject.getString(ApkSignerTool.KEY_RESULT_MSG);
			info.signature = GetSignature.getApkSignInfo(filePath);
			info.v2CheckErrorInfo = jsonobject.getString(ApkSignerTool.KEY_RESULT_MSG);
		}catch(Exception e){
			showFailedCheckResult("RET_GET_INFO_BAD","get apk info failed, throw an Exception:" + e.toString());
			return null;
		}
		
		System.out.println(info.isV2Signature);
		System.out.println(info.isV2SignatureOK);
		return info;
	}
	
	private static void showFailedCheckResult(String ret,String Msg){
		System.out.println("执行结果: 失败("+ ret+")"); 
		System.out.println("错误信息:" + Msg);
	}
}
